<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *门店管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','mobile','address','image','location','status','createtime');
	$list=pdo_getslice('jy_meifa_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$v){
			//$v['url']=custom_url('jy_meifa/store/detail',true).'&id='.$v['id'];
			$v['location']=json_decode($v['location'],true);
			$v['address']=$v['location']['province'].$v['location']['city'].$v['location']['district'].$v['address'];
		}
	}else{
		$list = array(
			array('title'=>'深圳龙华民治店')
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'门店列表'),
			array('op'=>'edit','title'=>'添加门店'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'门店名称','type'=>'string'),
			array('name'=>'image','title'=>'门店图标','type'=>'image'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'门店名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	set_html('framess',$this->menu());
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_meifa_store',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$item['location']=json_decode($item['location']);
	$item['lat_lng']=json_decode($item['lat_lng']);
	$item['onlinetime']=json_decode($item['onlinetime']);
    $return['item']=$item;
	$return['pcd']=cfc('tools')->pcd();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回门店列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'门店名称','type'=>'string'),
					array('name'=>'image','title'=>'门店图标','type'=>'image'),
					array('name'=>'mobile','title'=>'电话','type'=>'string','help'=>''),
					array('name'=>'location','title'=>'地区','type'=>'pcd','options'=>'pcd'),
					array('name'=>'address','title'=>'详细地址','type'=>'string'),
					array('name'=>'onlinetime','title'=>'营业时间','type'=>'time_hour','help'=>'营业时间段'),
					array('name'=>'lat_lng','title'=>'坐标','type'=>'lat_lng'),
					array('name'=>'remark','title'=>'门店介绍','type'=>'richtext'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			),
		)
	);


	$return['status']=array('0'=>'即将开业','1'=>'营业中','2'=>'暂停营业');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	//exi(json_encode($postdata['lat_lng']),'error');
	$data=array(
		'title'=>trim($postdata['title']),
		'mobile'=>intval($postdata['mobile']),
		'image'=>trim($postdata['image']),
		'location'=>ijson_encode($postdata['location']),
		'lat_lng'=>json_encode($postdata['lat_lng']),
		'onlinetime'=>json_encode($postdata['onlinetime']),
		'address'=>trim($postdata['address']),
		'remark'=>trim($postdata['remark']),
		'status'=>intval($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_meifa_store',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_meifa_store',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_meifa_store',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的门店！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_meifa_store',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}